package com.dxy.multids.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dxy.multids.mapper.OrderInfoMapper;
import com.dxy.multids.pojo.OrderInfo;
import com.dxy.multids.service.OrderInfoService;
import org.springframework.stereotype.Service;

@Service
public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo>  implements OrderInfoService {

    // @DS("master") // 默认也可以不写
    @Override
    public OrderInfo addOrder(Long userId, String orderName) {
        OrderInfo order = new OrderInfo();
        order.setUserId(userId);
        order.setOrderName(orderName);
        baseMapper.insert(order);
        return order;
    }

    // 读写数据库分离，主数据库写，从数据库读，主从复制使用MySQL自身机制实现
    @DS("dxy2")
    @Override
    public IPage<OrderInfo> getOrderByUserId(Long userId, Long pageNum, Long pageSize) {
        if (pageNum == null || pageNum <= 0){
            pageNum = 1L;
        }
        if (pageSize == null || pageSize <= 0){
            pageSize = 10L;
        }
        IPage<OrderInfo> page = new Page<>(pageNum, pageSize);
        return baseMapper.selectPage(page,
                new LambdaQueryWrapper<OrderInfo>().eq(userId != null, OrderInfo::getUserId, userId));
    }
}
